<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>hipac数据抓取</title>
<link rel="stylesheet" href="/layui/css/layui.css">
<script type="text/javascript" src="/js/jqueryui/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="/js/app/hipac/global.js"></script>
<script type="text/javascript" src="/layui/layui.js"></script>
<script type="text/javascript" src="/js/lib/autosize.min.js"></script>
<style>
body {
	font-family: 'monaco';
}

.center {
	margin-top: 100px;
	margin-left: auto;
	margin-right: auto;
}

.button1 {
	margin-top: 50px;
	margin-left: 60px;
}
</style>
</head>
<body>
	<table class="center">
		<tr>
			<td>API</td>
			<td id="api" bgcolor="black" style="color: white">//api.hipac.cn/process/prod/1.0.2/mall.item.searchItemListWithFlashBuyAct.pc/</td>
		</tr>
		<tr>
			<td>sign参数</td>
			<td id="sign" bgcolor="black" style="color: white"></td>
		</tr>
		<tr>
			<td>t参数</td>
			<td id="time" color="red" bgcolor="black" style="color: white"></td>
		</tr>
		<tr>
			<td>data参数</td>
			<td id="data" bgcolor="black" style="color: white"></td>
		</tr>
		<tr>
			<td colspan="2">
				<form class="layui-form" action="javascript:;">
					<div class="layui-form-item">
						<label class="layui-form-label">抓取栏目</label>
						<div class="layui-input-block">
							<input type="radio" name="itemtype" lay-filter="itemtype"
								value="2" title="国内仓发货"> <input type="radio"
								name="itemtype" lay-filter="itemtype" value="3" title="海外直邮">
							<input type="radio" name="itemtype" lay-filter="itemtype"
								value="1" title="保税区直供">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">Cookie</label>
						<div class="layui-input-block">
							<textarea id="cookie" placeholder="请通过浏览器获取Cookie粘贴至此"
								class="layui-textarea"></textarea>
						</div>
					</div>
					<div class="layui-form-item">
						<div class="layui-input-block">
							<button id="start" class="layui-btn">
								<i class="layui-icon">&#xe618;</i>开始抓取
							</button>
							<button id="updateCookie" class="layui-btn layui-btn-normal">
								<i class="layui-icon">&#xe614;</i>更新cookie
							</button>
							<button id="cleardb" class="layui-btn layui-btn-danger">
								<i class="layui-icon">&#x1006;</i>清空数据库
							</button>
							<button type="button" class="layui-btn" id="uploadTemplate">
								<i class="layui-icon">&#xe67c;</i>生成Excel表格
							</button>
						</div>
					</div>
					<div class="layui-form-item">
						<div class="layui-input-block">
							<button id="totalCount" class="layui-btn">
								共：0
							</button>
						</div>
					</div>
				</form>
			</td>
		</tr>
		<tr>
			<td colspan="2">&emsp;</td>
		</tr>
		<tr>
			<td colspan="2">
				<div class="layui-progress layui-progress-big" lay-showPercent="yes"
					lay-filter="allprogress">
					<div class="layui-progress-bar layui-bg-green" lay-percent="0%"></div>
				</div>
			</td>
		</tr>
		<tr>
			<td colspan="2">&emsp;</td>
		</tr>
		<tr>
			<td colspan="2">
				<div class="layui-progress layui-progress-big"
					lay-filter="detailprogress">
					<div class="layui-progress-bar layui-bg-orange" lay-percent="0%"></div>
				</div>
			</td>
		</tr>
	</table>
	<script type="text/javascript">
		layui.use([ 'element', 'layer', 'form', 'upload' ], function() {
			var form = layui.form;
			var upload = layui.upload;
			var listurl = "http://api.hipac.cn/process/prod/1.0.2/mall.item.searchItemListWithFlashBuyAct.pc/";
			var detailurl = "http://api.hipac.cn/process/prod/1.0.2/mall.item.searchItemListWithFlashBuyAct.pc/";
			var element = layui.element;
			var uploadInst = upload.render({
				elem : '#uploadTemplate',
				url : '/hipac/uploadtemplate.json',
				accept : 'file',
				done : function(res) {
					layer.alert(res.message);
					
				},
				error : function() {
					//请求异常回调
				}
			});
			$('#start').click(function(e) {
				get(1, true);
				getTotalCount();
			});
			$('#totalCount').click(function(){
				getTotalCount();
			});
			getTotalCount();
			function getTotalCount(){
				$.ajax({
				    type: 'POST',
				    url: '/hipac/getTotalCount.json',
				    success: function (data) {
				        if(data.success){
				        	$('#totalCount').text('共：'+data.message+'条');
				        }else{
				            layer.msg(data.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
				        }
				    },
				    error: function(data) {
				        layer.msg(data.responseText,{icon: 5,anim: 6,time: 0,btn:['确定']});
				    },
				    dataType: "json"
				});
			}
			$('#updateCookie').click(function() {
				$.ajax({
					type : 'POST',
					url : '/hipac/updateCookie.json',
					data : {
						cookieValue : $('#cookie').val()
					},
					success : function(data) {
						if (data.success) {
							layer.msg(data.message);
						} else {
							layer.msg(data.message, {
								icon : 5,
								anim : 6,
								time : 0,
								btn : [ '确定' ]
							});
						}
					},
					error : function(data) {
						layer.msg(data.responseText, {
							icon : 5,
							anim : 6,
							time : 0,
							btn : [ '确定' ]
						});
					},
					dataType : "json"
				});
			});
			$.ajax({
				type : 'POST',
				url : '/hipac/getCookie.json',
				success : function(data) {
					if (data.success) {
						$('#cookie').text(data.message);
						autosize($('#cookie'));
					} else {
						layer.msg(data.message, {
							icon : 5,
							anim : 6,
							time : 0,
							btn : [ '确定' ]
						});
					}
				},
				error : function(data) {
					layer.msg(data.responseText, {
						icon : 5,
						anim : 6,
						time : 0,
						btn : [ '确定' ]
					});
				},
				dataType : "json"
			});
			$('#cleardb').click(function() {
				$.ajax({
					type : 'POST',
					url : '/hipac/cleardb.json',
					success : function(data) {
						if (data.success) {
							layer.msg('已清空');
							getTotalCount();
						} else {
							layer.msg(data.message, {
								icon : 5,
								anim : 6,
								time : 0,
								btn : [ '确定' ]
							});
						}
					},
					error : function(data) {
						layer.msg(data.responseText, {
							icon : 5,
							anim : 6,
							time : 0,
							btn : [ '确定' ]
						});
					},
					dataType : "json"
				})
			});
	
			function get(pageNo, digui) {
				var itemselected = $("input[name='itemtype']:checked").val();
				if (itemselected == undefined) {
					layer.msg('没选择抓取栏目',{icon: 5,anim: 6,time: 0,btn:'确定'});
					return;
				}
				var list = {
					itemTypes : "",
					pageNo : 1,
					pageSize : 50,
					searchSource : "channel",
					sortType : "3",
					source : "pc"
				};
	
				list.itemTypes = itemselected;
	
				var detail = {
					itemId : "",
					source : "pc"
				};
	
				list.pageNo = pageNo;
	
				layer.msg(list.itemTypes + '，开始偷数据！');
				var sign = Yt.handlerAjaxData(listurl, list);
				$('#sign').text(sign.sign);
				$('#time').text(sign.t);
				$('#data').text(sign.data);
				$.ajax({
					type : 'POST',
					url : '/hipac/list.json',
					data : {
						sign : sign.sign,
						t : sign.t,
						data : sign.data
					},
					success : function(data) {
						if (data.success) {
							pageSize = data.pageSize;
							totalCount = data.totalCount;
	
							var detailid_index = 0
							var interval = setInterval(function() {
								var bfb1 = (detailid_index / data.listID.length) * 100
								element.progress('detailprogress', bfb1 + '%')
								if (detailid_index >= data.listID.length) {
									clearInterval(interval);
									return;
								}
								detail.itemId = data.listID[detailid_index];
								var detailsign = Yt.handlerAjaxData(detailurl, detail);
								$('#sign').text(detailsign.sign);
								$('#time').text(detailsign.t);
								$('#data').text(detailsign.data);
								getDetail(detailsign.sign, detailsign.t, detailsign.data);
								detailid_index++;
							}, 1000);
							if (digui) {
								for (var tmpPageNo = data.pageNo + 1; tmpPageNo <= (totalCount / pageSize); tmpPageNo++)
								//for(var tmpPageNo = data.pageNo+1;tmpPageNo<=3;tmpPageNo++)
								{
									var bfb = (tmpPageNo / (totalCount / pageSize)) * 100
									element.progress('allprogress', bfb + '%')
									get(tmpPageNo, false);
								}
							}
						} else {
							layer.msg(data.message, {
								icon : 5,
								anim : 6,
								time : 0,
								area : [ '500px', '300px' ],
								btn : '确定'
							});
						}
					},
					error : function(data) {
						//alert(data.responseText);
						layer.msg(data.responseText, {
							icon : 5,
							anim : 6,
							time : 0,
							area : [ '500px', '300px' ],
							btn : '确定'
						});
					},
					dataType : "json"
				})
			}
			function getDetail(sign, t, data) {
				$.ajax({
					type : 'POST',
					url : '/hipac/detail.json',
					data : {
						sign : sign,
						t : t,
						data : data
					},
					success : function(data) {
						if (data.success) {
	
						} else {
							layer.msg(data.message, {
								icon : 5,
								anim : 6,
								time : 0,
								btn : [ '确定' ]
							});
						}
					},
					error : function(data) {
						layer.msg(data.responseText, {
							icon : 5,
							anim : 6,
							time : 0,
							btn : [ '确定' ]
						});
					},
					dataType : "json"
				})
			}
		});
	</script>
</body>
</html>